<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/10/20
 * Time: 15:04
 */
header("content-type:text/html;charset=utf-8");
require COMMON_PATH.'logTool.class.php';
require COMMON_PATH.'message.class.php';
class UserAction extends Action
{
    public function login(){
    //定义返回数据
    $back = null;
    //获取登录code
    $js_code = $_REQUEST['code'];
    //登录参数
    $appid = "wxff072ab902fe845c";
    $secret = "";
    //登录地址
    $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$js_code}&grant_type=authorization_code";
    //获取到的用户信息
    $data = file_get_contents($url);
    $data = json_decode($data);
    //数据解析
    $errcode = $data['errcode'];
    if ($errcode == 0) {
        $openid = $data['openid'];
        $session_key = $data['session_key'];
        $unionid = $data['unionid'];
        //存入客户信息数据库
        $user = M('customer');
        $add_data = array(
            'openid' => $openid,
            'session_key' => $session_key,
            'unionid' => $unionid
        );
        if ($user->add($add_data)){
            $back_data = array(
                'openid' => $openid,
                'session_key' => $session_key,
                'unionid' => $unionid
            );
            $back = message::success('success',$back_data);
        } else {
            $back_data = message::fail('fail');
        }
    } else {
        //获取失败，返回错误消息
        $errMsg = $data['errMsg'];
        $err_data = array('errcode' => $errcode,'errMsg' => $errMsg);
        $back = message::fail('error',$err_data);
    }
    //记录返回消息
    logTool::logCat("Login返回信息: ".$back);
    echo $back;
}
}